from daos import get_db_session, get_update_contents
from models.album_episode_info import AlbumEpisodeInfo


def add_album_episode_info(model_orm: AlbumEpisodeInfo, is_commit=True):
    session = get_db_session()

    session.add(model_orm)
    session.commit() if is_commit else session.flush()
    return model_orm


def query_album_episode_info(pk):
    return get_db_session().query(AlbumEpisodeInfo).filter(AlbumEpisodeInfo.id == pk).first()


def query_album_episode_info_by_album_id(album_id, pk):
    filter_s = []
    filter_s.append(AlbumEpisodeInfo.id == pk)
    filter_s.append(AlbumEpisodeInfo.album_info_id == album_id)
    return get_db_session().query(AlbumEpisodeInfo).filter(*filter_s).first()


def update_album_episode_info(user_orm: AlbumEpisodeInfo, *args, is_commit=True):
    session = get_db_session()
    update_contents = get_update_contents(user_orm, *args)
    session.query(AlbumEpisodeInfo).filter(AlbumEpisodeInfo.id == user_orm.id).update(update_contents)
    session.commit() if is_commit else session.flush()
    return


def query_album_episode_info_with_page(data_param):
    filter_s = []

    if data_param.get('album_info_id'):
        filter_s.append(AlbumEpisodeInfo.album_info_id == data_param.get('album_info_id'))

    if data_param.get('title'):
        filter_s.append(AlbumEpisodeInfo.title.contains(data_param.get('title')))

    if data_param.get('seq_index'):
        filter_s.append(AlbumEpisodeInfo.seq_index == data_param.get('seq_index'))

    if data_param.get('dy_episode_id'):
        filter_s.append(AlbumEpisodeInfo.dy_episode_id == data_param.get('dy_episode_id'))

    session = get_db_session()
    return session.query(AlbumEpisodeInfo) \
        .filter(*filter_s) \
        .order_by(AlbumEpisodeInfo.seq_index.asc()) \
        .paginate(page=data_param.get('page'), per_page=data_param.get('page_size'), error_out=False)


def query_album_episode_info_s(data_param):
    filter_s = []

    if data_param.get('album_info_id'):
        filter_s.append(AlbumEpisodeInfo.album_info_id == data_param.get('album_info_id'))

    if data_param.get('title'):
        filter_s.append(AlbumEpisodeInfo.title.contains(data_param.get('title')))

    if data_param.get('seq_index'):
        filter_s.append(AlbumEpisodeInfo.seq_index == data_param.get('seq_index'))

    if data_param.get('dy_episode_id'):
        filter_s.append(AlbumEpisodeInfo.dy_episode_id == data_param.get('dy_episode_id'))

    session = get_db_session()
    return session.query(AlbumEpisodeInfo) \
        .filter(*filter_s) \
        .order_by(AlbumEpisodeInfo.seq_index.asc()) \
        .all()


def query_album_episode_info_count(data_param):
    filter_s = []

    if data_param.get('album_info_id'):
        filter_s.append(AlbumEpisodeInfo.album_info_id == data_param.get('album_info_id'))

    if data_param.get('title'):
        filter_s.append(AlbumEpisodeInfo.title.contains(data_param.get('title')))

    if data_param.get('seq_index'):
        filter_s.append(AlbumEpisodeInfo.seq_index == data_param.get('seq_index'))

    if data_param.get('dy_episode_id'):
        filter_s.append(AlbumEpisodeInfo.dy_episode_id == data_param.get('dy_episode_id'))

    session = get_db_session()
    return session.query(AlbumEpisodeInfo).filter(*filter_s).count()
